package org.example.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.example.config.JwtConfig;
import org.example.model.User;
import org.example.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component
public class CheckTokenUtil {
    @Autowired
    private UserRepository userRepository;
    private final JwtConfig jwtConfig;
    private final GetUidUtil getUidUtil;

    public CheckTokenUtil(JwtConfig jwtConfig, GetUidUtil getUidUtil) {
        this.jwtConfig = jwtConfig;
        this.getUidUtil = getUidUtil;
    }

    //检查长Token是否过期
    public Boolean checkToken(){
        // 获取当前请求
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        // 从请求头中获取Authorization的值。
        String authorizationHeader = request.getHeader("Authorization");

        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
            String token = authorizationHeader.substring(7); // 去掉 "Bearer " 前缀
            // 移除可能存在的大括号
            if (token.startsWith("{") && token.endsWith("}")) {
                token = token.substring(1, token.length() - 1);
            }
            try {
                Claims claims = Jwts.parser()
                        .setSigningKey(jwtConfig.getJwtSecret())
                        .parseClaimsJws(token)
                        .getBody();
                Date expiration = claims.getExpiration(); // 获取Token过期时间
                Date currentTime = new Date(System.currentTimeMillis()); // 当前时间
                if (currentTime.after(expiration)) {
                    // Token过期
                    return true;
                }
            } catch (Exception e) {
                // Token解析失败
                return true;
            }
        }
        return false;
    }

    //检查短Token是否过期
    public Boolean checktemporaryToken(){
        // 获取当前请求
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        // 从请求头中获取Authorization的值。
        String authorizationHeader = request.getHeader("Authorization");


        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
            String temporaryToken = authorizationHeader.substring(7); // 去掉 "Bearer " 前缀
            // 移除可能存在的大括号
            if (temporaryToken.startsWith("{") && temporaryToken.endsWith("}")) {
                temporaryToken = temporaryToken.substring(1, temporaryToken.length() - 1);
            }
            try {
                Claims claims = Jwts.parser()
                        .setSigningKey(jwtConfig.getJwtSecret())
                        .parseClaimsJws(temporaryToken)
                        .getBody();
                Date expiration = claims.getExpiration(); // 短Token过期时间
                Date currentTime = new Date(System.currentTimeMillis()); // 当前时间
                if (currentTime.after(expiration)) {
                    // Token过期
                    return true;
                }
            } catch (Exception e) {
                // Token解析失败
                return true;
            }
        }
        return false;
    }

    public User checkUid(){
        String uid = getUidUtil.getUid();
        User user = userRepository.findByOpenid(uid.toString());
        return user;
    }

}
